<?php

namespace App\Admin\Controllers;

use App\Models\Classroom;
use App\Models\Campus;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class ClassroomController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header(trans('admin.classroom'));
            $content->description(trans('admin.list'));
            
            $content->body($this->grid()->render());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header(trans('admin.classroom'));
            $content->description(trans('admin.edit'));

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header(trans('admin.classroom'));
            $content->description(trans('admin.create'));

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Classroom::class, function (Grid $grid) {
            $grid->id(trans('admin.ID'))->sortable();
            $grid->name(trans('admin.name'))->sortable();
            $grid->addr(trans('admin.addr'));
            $grid->Campus()->name(trans('admin.campus_id'))->sortable();

            $grid->actions(function ($actions) {
                $actions->disableView();
                $actions->disableDelete();
            });
            //禁用导出按钮
            $grid->disableExport();
            $grid->tools(function ($tools) { 
                //关闭批量删除 
                $tools->batch(function ($batch) { 
                    $batch->disableDelete(); 
                });
            });

            //筛选弹出框字段
            $grid->filter(function ($filter) {
                // 去掉默认的id过滤器
                $filter->disableIdFilter();
                
                $filter->like('name',trans('admin.name'));
                $filter->like('addr',trans('admin.addr'));
                $filter->equal('campus_id', trans('admin.campus_id'))->select(Campus::all()->pluck('name', 'id'));
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Classroom::class, function (Form $form) {
            $form->display('id', 'ID');

            $form->text('name', trans('admin.name'))->rules('required')->addElementLabelClass('must-input');
            $form->text('addr', trans('admin.addr'))->rules('required')->addElementLabelClass('must-input');
            $form->select('campus_id', trans('admin.campus_id'))->options(Campus::all()->pluck('name', 'id'))->addElementLabelClass('must-input'); 
            
            //去掉添加/编辑表单的重置按钮
            $form->disableReset();
        });
    }
}
